﻿/// <reference path="jquery.HimallLinkage.nocreate.js" />
(function ($) {
    function bindAddProductsBtn(c, opt) {
        $(c).find('#SelectProduct').click(function () {
            $.productSelector.show(null, function (selectedProducts) {
                var ids = [];
                $.each(selectedProducts, function () {
                    $('#imgProduct').attr('src', this.imgUrl);
                    $(opt.returnTag).val(this.id);
                });
                
            }, 'selleradmin', false);
        });
    }
    function bindPhone(c, opt) {
        $('#tContent').val(opt.defaultContent);
        $(c).find('#tContent').change(function () {           
            $(opt.returnTag).val($('#tContent').val());           
        });
    }

	//检查上传的图片格式
    function  BindCategory(c,opt) {       
        $(c).find('select').HimallLinkage({
            url: '/selleradmin/category/getCategory',
            enableDefaultItem: true,
            defaultItemsText: '请选择',
            onChange: function (level, value) {
                var categoryId = value;
                if (level > 0 && !value) {
                    var parentLevel = level - 1;
                    categoryId = $(c).find('select').HimallLinkage('value', parentLevel);
                    if (parentLevel > 0 && !categoryId) {
                        parentLevel--;
                        categoryId = $(c).find('select').HimallLinkage('value', parentLevel);
                    }
                }
                $(opt.returnTag).val(categoryId);
            }
        });
    }

	function bindEvent(target, opts) {
	    var type = Number(opts.type);
			
	    switch(type)
	    {
	        case 0://无跳转
	          
	            break;
	        case 1://分类	           
	            BindCategory(target, opts);
	            break;
	        case 2://电话
	            bindPhone(target, opts);
	            break;
	        case 3://商品
	            bindAddProductsBtn(target, opts);
	          
	            break;
	        case 4://优惠卷
	          
	            break;
	        case 5://限时购
	          
	            break;
	        case 6://限时购列表
	          
	            break;
	        case 7://小程序
	           
	            break;
	        case 8://URL链接
	           
	            break;
	        case 9://组合购

	            break;
	        case 10://专题

	            break;
	        default: 
	            break;					
	    }	


	}

	$.fn.jumpTemplate = function (options, param) {
		if (typeof options == "string") {
			return $.fn.jumpTemplate.methods[options](this, param);
		}
		options = options || {};
		return this.each(function () {
			var opts = $.extend({}, $.fn.jumpTemplate.defaults, options);
			opts.view.render.call(opts.view, opts, $(this));			
			bindEvent(this, opts);			
		});
	};

	$.fn.jumpTemplate.methods = {
	   
	};

	var renderView = {
		render: function (opts, container) {
			var id = new Date().getTime();
			var type = Number(opts.type);
			
			switch(type)
			{
				case 0://无跳转
					$(container).hide();
					break;
			    case 1://分类
			        $(container).show();
					var html = ['<label class="col-sm-2 control-label fl" for="">分类:</label>'];					
					html.push(' <select class="form-control input-ssm"></select><select class="form-control input-ssm"></select><select class="form-control input-ssm"></select>');
					$(container).html(html.join(''));					
					break;
			    case 2://电话
			        $(container).show();
					var html = ['<label class="col-sm-2 control-label fl" for="">电话:</label>'];
					html.push('<div class="col-sm-3"><input id="tContent" type="text" class="form-control input-sm" /></div><div class="col-sm-3"><span class="help-default"></span></div>');
					$(container).html(html.join(''));					
					break;
			    case 3://商品
			        $(container).show();
					var html = ['<label class="col-sm-2 control-label fl" for="">商品:</label>'];
					html.push('<div class="col-sm-3"><img id="imgProduct" src=""/>&nbsp;&nbsp;<a class="btn btn-primary btn-sm" id="SelectProduct"><span class="glyphicon glyphicon-plus-sign"></span>&nbsp; 选择商品</a></div>');
					$(container).html(html.join(''));					
					break;
			    case 4://优惠卷
			        $(container).show();
					var html = ['<label class="col-sm-2 control-label fl" for="">优惠卷:</label>'];
					$(container).html(html.join(''));
					break;
			    case 5://限时购
			        $(container).show();
					var html = ['<label class="col-sm-2 control-label fl" for="">限时购:</label>'];
					$(container).html(html.join(''));
					break;
			    case 6://限时购列表
			        $(container).show();
					var html = ['<label class="col-sm-2 control-label fl" for="">限时购列表:</label>'];
					$(container).html(html.join(''));
					break;
			    case 7://小程序
			        $(container).show();
					var html = ['<label class="col-sm-2 control-label fl" for="">小程序:</label>'];
					html.push('<div class="col-sm-3"><input id="txtContent" type="text" class="form-control input-sm" /></div><div class="col-sm-3"><span class="help-default"></span></div>');
					$(container).html(html.join(''));
					break;
			    case 8://URL链接
			        $(container).show();
					var html = ['<label class="col-sm-2 control-label fl" for="">URL链接:</label>'];
					html.push('<div class="col-sm-3"><input id="txtContent" type="text" class="form-control input-sm" /></div><div class="col-sm-3"><span class="help-default"></span></div>');
					$(container).html(html.join(''));
					break;
			    case 9://组合购
			        $(container).show();
			        var html = ['<label class="col-sm-2 control-label fl" for="">组合购:</label>'];
			        html.push('<div class="col-sm-3"><input id="txtContent" type="text" class="form-control input-sm" /></div><div class="col-sm-3"><span class="help-default"></span></div>');
			        $(container).html(html.join(''));
			        break;
			    case 10://专题
			        $(container).show();
			        var html = ['<label class="col-sm-2 control-label fl" for="">专题:</label>'];
			        html.push('<div class="col-sm-3"><input id="txtContent" type="text" class="form-control input-sm" /></div><div class="col-sm-3"><span class="help-default"></span></div>');
			        $(container).html(html.join(''));
			        break;
				default: $(container).hide();
					break;					
			}	
		}
	};

	$.fn.jumpTemplate.defaults = {
		type: 0,//跳转类型
		title: '跳转内容',
		view: renderView,
		defaultContent: '',
		returnTag: ''
	};
})(jQuery);